package com.ciwong.epaper.modules.scan.dao;

import android.app.Activity;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.ciwong.epaper.modules.epaper.bean.EpaperInfo;
import com.ciwong.epaper.modules.me.bean.Service;
import com.ciwong.epaper.modules.scan.bean.EpaperQRInfo;
import com.ciwong.epaper.modules.scan.bean.PaperCodeInfo;
import com.ciwong.epaper.modules.scan.bean.QRInfo;
import com.ciwong.epaper.modules.scan.bean.QrBook;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.volley.BaseRequest;
import com.ciwong.libs.utils.volley.HttpRequest;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.utils.BaseRequestUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 扫一扫 相关网络请求
 *
 * @author bin
 * @version ciwong v.1.0 2015/7/23 19:53
 * @since ciwong v.1.0
 */
public class ScanRequest
{
    /**
     * 二维码验证
     *
     * @param activity
     *            上下文
     * @param action
     *            请求地址
     * @param callBack
     *            请求回调
     */
    public static void requestData(Activity activity, String action,
            final BaseCallBack callBack)
    {
        HttpRequest request = new HttpRequest(action,
                new Response.ErrorListener()
                {

                    @Override
                    public void onErrorResponse(VolleyError error)
                    {
                        if (callBack != null)
                        {
                            callBack.failed(error);
                        }
                    }
                }, new BaseRequest.ResponseCallback()
                {

                    @Override
                    public void error(int errorCode, String msg)
                    {
                        if (callBack != null)
                        {
                            callBack.failed(errorCode, msg);
                        }
                    }

                    @Override
                    public void success(String responseStr)
                    {
                        CWLog.e("debug", responseStr);
                        JSONObject jsonObj = null;
                        int ret = -1;
                        int errcode = -1;
                        try
                        {
                            jsonObj = new JSONObject(responseStr);
                            ret = jsonObj.getInt("ret");
                            errcode = jsonObj.getInt("errcode");
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                        if (ret == 0 && errcode == 0)
                        {
                            Gson gson = new Gson();
                            QRInfo qrInfo = null;
                            String values = null;
                            try
                            {
                                jsonObj = jsonObj.getJSONObject("data");
                                qrInfo = gson.fromJson(jsonObj.toString(),
                                        QRInfo.class);
                                values = jsonObj.getString("values");
                            }
                            catch (Exception e)
                            {
                                e.printStackTrace();
                            }
                            if (callBack != null)
                            {
                                callBack.success(qrInfo, values);
                            }
                        }
                    }

                });
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }

    /**
     * 扫码后下载zip包
     * 
     * @param action
     * @param callBack
     */
    public static void requestNewspaperQRInfo(String action,
            final BaseCallBack callBack)
    {
        HttpRequest request = new HttpRequest(action,
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError volleyError)
                    {
                        callBack.failed(volleyError);
                    }
                }, new BaseRequest.ResponseCallback()
                {
                    @Override
                    public void success(Object obj, int resultCode,
                            int errorCode, String msg)
                    {
                        if (callBack != null)
                        {
                            if (resultCode == 0 && errorCode == 0)
                            {
                                callBack.success(obj);
                            }
                            else
                            {
                                callBack.failed(errorCode, msg);
                            }
                        }
                    }

                    @Override
                    public void error(int errorCode, String msg)
                    {
                        if (callBack != null)
                        {
                            callBack.failed(errorCode, msg);
                        }
                    }
                });
        request.setResponseClazz(EpaperQRInfo.class);
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }

    /**
     * 判断资源和品牌是否匹配
     * @param brandId
     * @param productId
     * @param callBack
     * ADD BY young
     */

    public static void getQRandBrandResourceInfo(int brandId,String  productId,final BaseCallBack callBack){
        Map<String,String> params=new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, ScanAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION,
                ScanAction.ACTION_GET_QR_BRAND_RESOURCE_INFO);
        params.put("brandId",brandId+"");
        params.put("productId",productId);
        HttpRequest request=new HttpRequest(params,
        new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                callBack.failed(volleyError);
            }
        },new BaseRequest.ResponseCallback(){
            @Override
            public void success(Object obj, int resultCode, int errorCode, String msg) {
                if (resultCode == 0 && errorCode == 0)
                {
                    callBack.success(obj);
                }
                else
                {
                    callBack.failed(errorCode, msg);
                }
            }

            @Override
            public void error(int errorCode, String msg) {
                callBack.failed(errorCode, msg);
            }
        });
        Type responseType = new TypeToken<List<EpaperInfo.Server>>(){
        }.getType();
        request.setResponseType(responseType);
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }

    /**
     * 根据二维码获取书籍
     *
     * @param packageId
     *            书籍ID
     * @param callBack
     *            回调
     */
    public static void getBookInfo(String packageId,
                                         final BaseCallBack callBack)
    {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, ScanAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION,
                ScanAction.ACTION_GET_QR_BOOK_INFO);
        params.put("packageId", packageId);
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error)
                    {
                        callBack.failed(error);
                    }
                }, new BaseRequest.ResponseCallback()
        {

            @Override
            public void error(int errorCode, String msg)
            {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode,
                                int errorCode, String msg)
            {
                if (resultCode == 0 && errorCode == 0)
                {
                    callBack.success(obj);
                }
                else
                {
                    callBack.failed(errorCode, msg);
                }
            }
        });
        request.setResponseClazz(QrBook.class);
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }



    /**
     * 根据二维码获取离线包信息
     * 
     * @param url
     *            URL地址
     * @param callBack
     *            回调
     */
    public static void getQRResourceInfo(String url,
            final BaseCallBack callBack)
    {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, ScanAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION,
                ScanAction.ACTION_GET_QR_RESOURCE_INFO);
        params.put("url", url);
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener()
                {

                    @Override
                    public void onErrorResponse(VolleyError error)
                    {
                        callBack.failed(error);
                    }
                }, new BaseRequest.ResponseCallback()
                {

                    @Override
                    public void error(int errorCode, String msg)
                    {
                        callBack.failed(errorCode, msg);
                    }

                    @Override
                    public void success(Object obj, int resultCode,
                            int errorCode, String msg)
                    {
                        if (resultCode == 0 && errorCode == 0)
                        {
                            callBack.success(obj);
                        }
                        else
                        {
                            callBack.failed(errorCode, msg);
                        }
                    }
                });
        request.setResponseClazz(EpaperQRInfo.class);
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }

    /**
     * 根据二维码获取电子报品牌信息
     *
     * @param url
     *            二维码扫描出来的URL
     * @param callBack
     *            回调
     */
    public static void getQRNewspaperBrand(String url,
            final BaseCallBack callBack)
    {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, ScanAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION,
                ScanAction.ACTION_GET_QR_NEWSPAPER_BRAND);
        params.put("url", url);
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError volleyError)
                    {
                        callBack.failed(volleyError);
                    }
                }, new BaseRequest.ResponseCallback()
                {
                    @Override
                    public void error(int errorCode, String msg)
                    {
                        callBack.failed(errorCode, msg);
                    }

                    @Override
                    public void success(Object obj, int resultCode,
                            int errorCode, String msg)
                    {
                        if (resultCode == 0 && errorCode == 0)
                        {
                            callBack.success(obj);
                        }
                        else
                        {
                            callBack.failed(errorCode, msg);
                        }
                    }
                });
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(Service.class);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }

    /**
     * 根据试卷编码获取试卷信息
     *
     * @param codeId
     *            试卷编码
     * @param callBack
     *            回调
     */
    public static void paperCodeByInfo(int codeId, final BaseCallBack callBack)
    {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, ScanAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION,
                ScanAction.ACTION_PAPER_CODE_BY_INFO);
        params.put("codeId", codeId + "");
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError volleyError)
                    {
                        callBack.failed(volleyError);
                    }
                }, new BaseRequest.ResponseCallback()
                {
                    @Override
                    public void error(int errorCode, String msg)
                    {
                        callBack.failed(errorCode, msg);
                    }

                    @Override
                    public void success(Object obj, int resultCode,
                            int errorCode, String msg)
                    {
                        if (resultCode == 0 && errorCode == 0)
                        {
                            callBack.success(obj);
                        }
                        else
                        {
                            msg = errorCode == 5158 ? "试卷编码不存在！" : msg;
                            callBack.failed(errorCode, msg);
                        }
                    }
                });
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(PaperCodeInfo.class);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }
}
